﻿using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Web;
using System.Windows.Forms;
using DGM.Util.Helpers.OAuth_dotnet;

namespace DGM.Util.Dialog.OAuth
{
  public partial class OAuthorizeWindowDialog : Form
  {
    private IOAuth OAuth { get; set; }
    public string Token { get; set; }
    public string Verifier { get; set; }
    public string TokenSecret { get; set; }

    public OAuthorizeWindowDialog(IOAuth oAuth)
    {
      InitializeComponent();

      OAuth = oAuth;
      Token = OAuth.Token;
      TokenSecret = OAuth.ConsumerSecret;

      authBrower.Navigate(new Uri(OAuth.AuthorizationLink));
    }

    private void authBrower_Navigating(object sender, WebBrowserNavigatingEventArgs e)
    {
      if (e.Url.Scheme == "liconnect")
      {
        string queryParams = e.Url.Query;
        if (queryParams.Length > 0)
        {
          //Store the Token and Token Secret
          NameValueCollection qs = HttpUtility.ParseQueryString(queryParams);
          if (qs["oauth_token"] != null)
          {
            Token = qs["oauth_token"];
          }
          if (qs["oauth_verifier"] != null)
          {
            Verifier = qs["oauth_verifier"];
          }
        }
        Close();
      }           
    }
  }
}
